export * from './auth';

export function checkUpdate() {
  if (process.env.NODE_ENV === 'development') {
    return;
  }

  // #ifdef MP
  const updateManager = uni.getUpdateManager();

  updateManager.onCheckForUpdate(function (res: any) {
    // 请求完新版本信息的回调
    console.log(res.hasUpdate);
  });

  updateManager.onUpdateReady(function () {
    uni.showModal({
      title: '更新提示',
      content: '新版本已经准备好，是否重启应用？',
      success(res) {
        if (res.confirm) {
          // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
          updateManager.applyUpdate();
        }
      },
    });
  });

  updateManager.onUpdateFailed(function () {
    // 新的版本下载失败
  });
  // #endif
}

/**
 * 获取区间点的随机值
 */
export function getRandomNum(start: number, end: number) {
  return (Math.random() * (end - start) + start).toFixed();
}

// Math.random()*(n-m)+m;

/**
 * 将 >10000 的数字标记为 xW
 */
export function tranNumber(oldNumber: number, uniWord: string = '万'): string {
  if (!oldNumber) return '0';
  let point = 0; //保留小数点后几位
  if (oldNumber < 10000) {
    return oldNumber.toFixed(point);
  } else {
    let num = oldNumber.toFixed(0);
    return (Number.parseInt(num) / 10000).toFixed(point) + uniWord;
  }
}

export const isToday = (date: Date) => {
  //今天
  var d = new Date(date.toString().replace(/-/g, '/'));
  var todaysDate = new Date();
  if (d.setHours(0, 0, 0, 0) == todaysDate.setHours(0, 0, 0, 0)) {
    return true;
  } else {
    return false;
  }
};

/**
 *
 * @param date 日期
 * @param splitStr 分隔符
 * @returns
 */
export const formatDate = (date: Date, splitStr = '-') => {
  let year = date.getFullYear(); //得到年份
  let month = date.getMonth() + 1; //得到月份
  let day = date.getDate(); //得到日期
  return `${year}${splitStr}${month < 10 ? '0' + month : month}${splitStr}${day < 10 ? '0' + day : day}`;
};

// 获取min 到 max之间的随机整数
export const getRandomNumber = (min: number, max: number) => {
  return Math.floor(Math.random() * (max - min + 1)) + min;
};

export const getRandomNumberByWeight = (numbersWithWeights: any[]): number => {
  // 计算总权重
  const totalWeight = numbersWithWeights.reduce((sum: number, { weight }) => sum + weight, 0);

  // 生成随机数
  const randomValue = Math.random() * totalWeight;

  // 根据随机数选择数字
  let cumulativeWeight = 0;
  let num = 0;
  for (const { number, weight } of numbersWithWeights) {
    cumulativeWeight += weight;
    if (randomValue < cumulativeWeight) {
      return (num = number);
    }
  }
  return num;
};

/**
 * 格式化时间
 * @param { 时间毫秒值 } time
 * @returns
 */
export const getTimeStr = (time: number) => {
  let hour = 0;
  let minute = 0;
  let second = 0;
  let unitHour = 1000 * 60 * 60;
  let unitMinute = 1000 * 60;
  minute = Math.floor(time / unitMinute);
  second = Math.floor(time / 1000);
  if (time > unitHour) {
    hour = Math.floor(time / unitHour);
    minute = Math.floor((time % unitHour) / unitMinute);
    second = Math.floor((time % unitMinute) / 1000);
  }
  let strTime = time > unitMinute ? `${hour}小时${minute}分钟` : `${hour}小时${minute}分钟${second}秒`;
  return strTime;
};
